<?php


namespace App\Http\Controllers\Admin\UserLabel;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

class SoundLabelQuestionController extends BaseController
{
    /**
     * @api {get} /user/label-questions 问题列表
     * @apiVersion 0.1.0
     * @apiName 问题列表
     * @apiGroup 声音录制问题
     * @apiParam {Int}                     page  （选填）
     * @apiParam {Int}                     page_size 历史产出限制 （选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 1,
     * "current_page": 1,
     * "last_page": 1,
     * "list": [
     * {
     * "id": 9,
     * "content": "111",
     * "status": 1,//0禁用 1启用
     * "updated_at": "2021-10-22 20:37:23"
     * },
     * ]
     * },
     * "message": ""
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function list(Request $request)
    {
        $data = $request->only(['page', "page_size"]);

        $va = app('validator')->make($data, [
            'page'      => 'integer|min:1',
            'page_size' => 'integer|min:1',

        ]);

        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $data['page'] = $data['page'] ?? 1;
        $data['page_size'] = $data['page_size'] ?? 20;
        $list = app('model_user_sound_questions')
            ->orderBy('id', 'desc')
            ->paginate($data['page_size'], ['id', 'content', 'status', 'updated_at'], '', $data['page'])
            ->toArray();
        $result = [
            'total'        => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page'    => $list['last_page'] ?? 1,
            'list'         => $list['data'],

        ];

        return Response::sendData($result);
    }

    /**
     * @api {post} /user/label-questions 添加问题
     * @apiVersion 0.1.0
     * @apiName 添加问题
     * @apiGroup 声音录制问题
     * @apiParam {string}                     content  （必填）
     * @apiParam {Int}                     status  0禁用 1启用（必填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true,
     * "message": ""
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function add(Request $request)
    {
        $data = $request->only(['status', "content"]);
        $va = app('validator')->make($data, [
            'content' => 'required|string|max:255',
            'status'  => 'required|in:1,0',
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $res = app('model_user_sound_questions')->insert($data);
        if ($res) {
            return Response::sendData(true);
        } else {
            throw  new BasicException(10001, '添加失败');
        }
    }

    /**
     * @api {put} /user/label-questions 修改问题
     * @apiVersion 0.1.0
     * @apiName 添加问题
     * @apiGroup 声音录制问题
     * @apiParam {string}                     content  （必填）
     * @apiParam {Int}                     status  0禁用 1启用（必填）
     * @apiParam {Int}                     id  （必填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true,
     * "message": ""
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function edit(Request $request)
    {
        $data = $request->only(['id', 'content', "status"]);
        $va = app('validator')->make($data, [
            'id'      => 'required|integer|min:1',
            'content' => 'required|string|max:255',
            'status'  => 'required|in:1,0',
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $res = app('model_user_sound_questions')->where('id', $data['id'])->update($data);
        if ($res) {
            return Response::sendData(true);
        } else {
            throw  new BasicException(10001, '修改失败');
        }
    }

    /**
     * @api {delete} /user/label-questions 删除问题
     * @apiVersion 0.1.0
     * @apiName 删除问题
     * @apiGroup 声音录制问题
     * @apiParam {Int}                     id  （必填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true,
     * "message": ""
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function del(Request $request)
    {
        $data = $request->only(['id']);
        $va = app('validator')->make($data, [
            'id' => 'required|integer|min:1',
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $res = app('model_user_sound_questions')->where('id', $data['id'])->delete();
        if ($res) {
            return Response::sendData(true);
        } else {
            throw  new BasicException(10001, '删除失败');
        }
    }
}